<?php
//
define('MSG_TYPE_SMS', 1);
define('MSG_TYPE_WEIXIN_TEXT', 11);
define('MSG_TYPE_WEXXIN_TEMPLATE', 12);
define('ADMIN_USER_OPENID', 'ofRX9wsY7We3eCZHuRDbPIZyKM9Q');

/**
 * 创建 - 直接向手机号码发送短信
 * @param $mobile   接收短信的手机号码
 * @param $text     短信内容
 * @param $template 短信模板(阿里云后台短信服务设置的模板名称)
 * @return mixed    id
 */
function msg_sms_send_to_mobile( $mobile, $text, $template ) {
	$in_data = array(
		'mobile' => $mobile,
		'message' => $text,
		'template' => $template,
		'type' => MSG_TYPE_WEIXIN_TEXT,
		'ctime' => time(),
		'status' => 0
	);
	$id = db_insert('message', $in_data);

	return $id;
}

use Sms\Request\V20160927 as Sms;
require_once 'libs/aliyun_core/Config.php';
/**
 * 阿里云短信服务发送信息接口
 * @param string $templatecode    短信模板代码
 * @param string $mobile          手机号码
 * @param string $content         短信内容
 * @param string $product         产品名称
 * @param string $signname        签名名称
 * @return array
 */
function send_sms_by_aliyun($templatecode = 'SMS_53135078', $mobile = '', $content = '', $product = '天下秀', $signname = '注册验证'){
	$ret = array('status' => false, 'info' => '');
	$iClientProfile = DefaultProfile::getProfile("cn-hangzhou", Aliyun_Config::ACCESS_ID, Aliyun_Config::ACCESS_KEY);
	$client = new DefaultAcsClient($iClientProfile);
	$request = new Sms\SingleSendSmsRequest();
	$request->setSignName($signname);/*签名名称*/
	$request->setTemplateCode($templatecode);/*模板code*/
	$request->setRecNum($mobile);/*目标手机号*/
	$request->setParamString('{"code":"'.$content.'", "product":"'.$product.'"}');/*模板变量，数字一定要转换为字符串*/
	try {
		$response = $client->getAcsResponse($request);
		//$response->Model 存在发送成功
		if( ! empty($response->Model) ){
			$ret['status'] = true;
		}
	}
	catch (ClientException  $e) {
		$ret['info'] = '错误代码:'.$e->getErrorCode().' 错误信息:'.$e->getErrorMessage();
	}
	catch (ServerException  $e) {
		$ret['info'] = '错误代码:'.$e->getErrorCode().' 错误信息:'.$e->getErrorMessage();
	}
	return $ret;
}

//订单支付成功

function goods_buy_tx($user, $order){
	$app = weixin_get_app();
	$notice = $app->notice;
	$remark = '收货信息 ' . $order['contacts'] . ' ' . $order['mobile'] . ' ' . $order['address'];
	$messageId = $notice->send([
			'touser' => $user['openid'],
			'template_id' => 'uiv4oASmGzLZ_IO6UL9pLlFTDk1rnOMcu0xJHd0NGTI',
			'url' => wx_url('goods/orders/100'),
			'topcolor' => '#f7f7f7',
			'data' => [
					'first' => '订单详情',
					'orderMoneySum' => finance_amount_yuan($order['amount']).'元',
					'orderProductName' => $order['title'].' × '.$order['quantity'].$order['goods_danwei'],
					'Remark' => $remark,
			],
	]);
}



